package entity

import (
	"fmt"
	"time"

	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

// YcProfit 利润预测
type YcProfit struct {
	ID        int        `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	DRRQ      *time.Time `gorm:"column:DRRQ"`
	OrganID   int        `gorm:"column:OrganID"`
	OrganName string     `gorm:"column:OrganName"`
	MNLR      *float64   `gorm:"column:MNLR"`
}

//TableName 利润预测
func (YcProfit) TableName() string {
	return "T_JHCW_LRYC_DTIP"
}

// LrycAfterImport 导入后执行分户处理
func LrycAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	prams := make([]interface{}, 1)
	prams[0] = params["DRRQ"]
	tx.Exec("EXEC UP_JHCW_LRYC ?", prams...)
	return nil
}

//BeforeSave 保存前的触发器
func (i *YcProfit) BeforeSave(tx *gorm.DB) (err error) {
	sqlstr := "select id from T_Organization where OrganName=?"
	row := tx.Raw(sqlstr, i.OrganName).Row()
	var utid int
	row.Scan(&utid)
	if utid == 0 {
		return fmt.Errorf("指定的部门名称%s在系统中不存在", i.OrganName)
	}
	i.OrganID = utid
	return
}

func YcProfitEF() base.EntityFunc {
	return func() interface{} {
		entity := YcProfit{}
		return &entity
	}
}
func YcProfitESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []YcProfit
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("YcProfit", base.NewDefaultDao(YcProfitEF(), YcProfitESF(), nil))
	base.SetAfterCallBack("YcProfit", LrycAfterImport)
}
